# crawler/forms.py
from django import forms
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth.models import User

class RegistrationForm(UserCreationForm):
    def clean_inviter_code(self):
        code = self.cleaned_data.get('inviter_code')
        if code and not User.objects.filter(username=code).exists():
            raise forms.ValidationError("邀请人不存在")
        return code
    email = forms.EmailField(required=True)
    inviter_code = forms.CharField(  # 新增邀请码字段
        max_length=20,
        required=False,
        help_text="填写邀请人用户名（选填）"
    )

    class Meta:
        model = User
        fields = ('username', 'email', 'inviter_code', 'password1', 'password2')
